package net.rong.mobileim;

import android.annotation.SuppressLint;
import android.os.AsyncTask;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;

import net.x52im.mobileimsdk.android.ClientCoreSDK;
import net.x52im.mobileimsdk.android.conf.ConfigEntity;
import net.x52im.mobileimsdk.android.core.LocalDataSender;
import net.x52im.mobileimsdk.server.protocal.c.PLoginInfo;

import java.util.HashMap;
import java.util.Map;

@ReactModule(name = MobileImModule.NAME)
public class MobileImModule extends ReactContextBaseJavaModule {

    public static final String NAME = "MobileIM";

    public MobileImModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    @NonNull
    public String getName() {
        return NAME;
    }

    @Override
    public Map<String, Object> getConstants() {
        Map<String, Object> constants = new HashMap<>();
        constants.put("ON_LOGIN_RESPONSE", EventConst.ON_LOGIN_RESPONSE);
        constants.put("ON_LINK_CLOSE", EventConst.ON_LINK_CLOSE);
        constants.put("ON_KICK_OUT", EventConst.ON_KICK_OUT);
        constants.put("ON_RECEIVE_MESSAGE", EventConst.ON_RECEIVE_MESSAGE);
        constants.put("ON_ERROR_RESPONSE", EventConst.ON_ERROR_RESPONSE);
        constants.put("MESSAGES_LOST", EventConst.MESSAGES_LOST);
        constants.put("MESSAGES_BE_RECEIVED", EventConst.MESSAGES_BE_RECEIVED);
        return constants;
    }

    @ReactMethod
    public void enableProd() {
        ClientCoreSDK.DEBUG = false;
    }

    @ReactMethod
    public void init(String domain, Integer port, Promise promise) {
		ConfigEntity.serverIP = domain;
  		ConfigEntity.serverPort = port;

        ConfigEntity.setSenseMode(ConfigEntity.SenseMode.MODE_5S);

        //TCP帧最大默认长度6k

        // 【特别注意】请确保首先进行核心库的初始化（这是不同于iOS和Java端的地方)
        ClientCoreSDK.getInstance().init(getReactApplicationContext().getApplicationContext());

        // 设置事件回调
        ClientCoreSDK.getInstance()
            .setChatBaseEvent(new ChatBaseEventListener(getReactApplicationContext()));
        ClientCoreSDK.getInstance()
            .setChatMessageEvent(new ChatMessageEventListener(getReactApplicationContext()));
        ClientCoreSDK.getInstance()
            .setMessageQoSEvent(new MessageQoSEventListener(getReactApplicationContext()));

        promise.resolve("");
    }

    @ReactMethod
    public void login(String userId,
                      String token,
                      Promise promise) {
        new LocalDataSender.SendLoginDataAsync(new PLoginInfo(userId, token)) {

            @Override
            protected void fireAfterSendLogin(int code) {
                promise.resolve(code);
            }

        }.execute();
    }

    @ReactMethod
    public void loginWithExtra(String userId,
                               String token,
                               String extra,
                               Promise promise) {
        new LocalDataSender.SendLoginDataAsync(new PLoginInfo(userId, token, extra)) {

            @Override
            protected void fireAfterSendLogin(int code) {
                promise.resolve(code);
            }

        }.execute();
    }

    @ReactMethod
    public void hasLogin(Promise promise) {
        boolean res = ClientCoreSDK.getInstance().isLoginHasInit();
        promise.resolve(res);
    }

    @SuppressLint("StaticFieldLeak")
    @ReactMethod
    public void send(String content,
                     String toId,
                     String fp,
                     int typeU,
                     Promise promise) {
        new AsyncTask<Integer, Integer, String>() {

            @Override
            protected String doInBackground(Integer... integers) {
                int code = LocalDataSender.getInstance().sendCommonData(content, toId, fp, typeU);

                promise.resolve(code);

                return "";
            }

        }.execute();
    }

    @SuppressLint("StaticFieldLeak")
    @ReactMethod
    public void logout(Promise promise) {
        new AsyncTask<Integer, Integer, String>() {

            @Override
            protected String doInBackground(Integer... integers) {
                int code = LocalDataSender.getInstance().sendLoginout();

                promise.resolve(code);

                return "";
            }

        }.execute();
    }

}
